package com.lwy.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwy.goods.dao.TbAdMapper;
import com.lwy.goods.pojo.TbAd;
import com.lwy.goods.service.TbAdService;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class TbAdServiceImpl extends ServiceImpl<TbAdMapper, TbAd> implements TbAdService {

    @Override
    public PageInfo<TbAd> page(String name,Integer pageNum, Integer pageSize) {
        if(null==pageNum){
            //当前页
            pageNum=1;
        }
        if(null==pageSize){
            //一页多少数据
            pageSize=10;
        }
        QueryWrapper<TbAd> wrapper = new QueryWrapper<>();
        //按广告名称查询
        if(null!=name&&!"".equals(name)){
            wrapper.like("name",name);
        }
        //分页
        PageHelper.startPage(pageNum,pageSize);
        //将分页得到的数据封装成pageInfo对象
        PageInfo<TbAd> pageInfo = new PageInfo<>(baseMapper.selectList(wrapper));
//        //从pageInfo中获取list集合
//        List<TbSku> list = pageInfo.getList();
        return  pageInfo;
    }

    public int saveTbAd(TbAd TbAd) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = sdf.parse(TbAd.getStart_time());
        String format = sdf.format(parse);
        TbAd.setStart_time(format);
        Date parse1 = sdf.parse(TbAd.getEnd_time());
        String format1 = sdf.format(parse1);
        TbAd.setEnd_time(format1);
        int insert = baseMapper.insert(TbAd);
        return insert;
    }
    public int updateTbAd(TbAd TbAd) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = sdf.parse(TbAd.getStart_time());
        String format = sdf.format(parse);
        TbAd.setStart_time(format);
        Date parse1 = sdf.parse(TbAd.getEnd_time());
        String format1 = sdf.format(parse1);
        TbAd.setEnd_time(format1);
        return baseMapper.updateById(TbAd);
    }
}
